package com.example.demo;

/**
 * @author libo
 * @ClassName TestInteger
 * @date 2019/2/28 20:20
 */
public class TestInteger {
    public static void main(String[] args) {
        //System.out.println(reverseInt2(Integer.MAX_VALUE));
        System.out.println(istPalindromenumber(121));
    }
    public static  int reverseInt(int num){
        if(Math.abs(num) >= Integer.MAX_VALUE || Math.abs(num)==Integer.MIN_VALUE){
            return  0;
        }
        int  a  = Math.abs(num);
        String[] split = String.valueOf(Math.abs(num)).split("");
        StringBuffer buffer = new StringBuffer() ;
        for (int i = split.length-1; i >= 0; i--) {
            if(!split[i] .equals("0")){buffer.append(split[i]);}
        }
        if(buffer.toString().length()== 0){
              return  0;   
        }
        if(Long.valueOf(buffer.toString()) > Integer.MAX_VALUE ){
                return 0;
        }
        int result = num > 0 ? Integer.valueOf(buffer.toString()):-1* Integer.valueOf(buffer.toString());
        return  result;
    }

    public static  int reverseInt2(int x){
        int rev = 0;
        while (x != 0) {
            int pop = x % 10;
            x /= 10;
            if (rev > Integer.MAX_VALUE/10 || (rev == Integer.MAX_VALUE / 10 && pop > 7)) return 0;
            if (rev < Integer.MIN_VALUE/10 || (rev == Integer.MIN_VALUE / 10 && pop < -8)) return 0;
            rev = rev * 10 + pop;
        }
        return rev;
    }

    public static boolean istPalindromenumber(int x){
        if(x < 0 || (x %10 == 0 && x/10 !=0)){
            return  false;
        }
        int revertedNumber = 0;
        while (x > revertedNumber){
            revertedNumber = revertedNumber * 10 + x % 10;
            x /= 10;
        }
        return x == revertedNumber || x == revertedNumber /10;
    }
}
